package com.arron.algorithm.leetcodetop100.滑动窗口;

import javax.print.DocFlavor;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 无重复字符的最长子串
 */
public class 无重复字符的最长子串 {


    //解法1： 滑动窗口
    public int lengthOfLongestSubstring(String s) {

        Map<Character,Integer> map = new HashMap<>();
        int maxLen= 0;
        int left = 0;
        for (int i = 0; i < s.length(); i++) {

            char c = s.charAt(i);
            //a b c a b c b b
            //a b b a
            if (map.containsKey(c)){
                left =Math.max(left, map.get(c)+1);
            }
            map.put(c,i);
            maxLen = Math.max(maxLen,i-left+1);



        }



        return maxLen;
    }


    //a b c a
    //a b b a
    public int lengthOfLongestSubstring2(String s) {

        if (s ==null || s.equals("")){
            return 0;
        }

        Map<Character,Integer> window = new HashMap<>();

        int l=0,r=0;

        int res = 0;

        while (r < s.length()){

            char c = s.charAt(r);
            r++;

            window.put(c,window.getOrDefault(c,0)+1);

            //更新左指针的位置，一直到无重复元素
            while (window.get(c)>1){

                char d = s.charAt(l);

                l++;
                window.put(d,window.get(d)-1);

            }
            //这里更新最大长度
            res = Math.max(res, r-l);
        }


        return res;

    }



    public static void main(String[] args) {

       String s = "cbaebabacd";
               String p = "abc";

        System.out.println(s.length() - p.length());


        int count = 10;
        while (count>=0){

            System.out.println(new Random().nextFloat());

            count--;
        }


    }




}
